<template>
  <div>
    <input placeholder="输入关键字搜索" v-model.trim="text">
    <table class="my-table">
      <thead>
        <tr>
          <th>就诊日期</th>
          <th>医生姓名</th>
          <th>诊断结果</th>
          <th>处方信息</th>
          <th>操作</th>
        </tr>
      </thead>
  <AllList :list="textList" @openDetail="openDetail" />
    </table>
    <DetailList v-show="flag" :renderList="renderList" @changeFlag="changeFlag" />
  </div>
</template>
<script>
import AllList from '@/components/AllList.vue'
import DetailList from '@/components/DetailList.vue'
export default {
  components:{
    AllList,DetailList
  },
  data(){
    return{
    list:[{id:1,utime:'2020-12-1',doctor:'李四',results:'头疼',medicine:'感冒药',},
     {id:2,utime:'2020-1-11',doctor:'张三',results:'肚子痛',medicine:'肠胃药',},
     {id:3,utime:'2020-3-20',doctor:'王二麻子',results:'发烧',medicine:'退烧药',},
     {id:4,utime:'2020-1-11',doctor:'张三',results:'肚子痛',medicine:'肠胃药',},
     ],
     text:'',
     flag:false,
     id:''
    }
  },
  methods:{
    openDetail(id){
      this.flag=!this.flag
     this.id=id
    },
    changeFlag(){
   
      this.flag=!this.flag
      this.id=''
    }
  },
  computed:{
    textList(){
      if(this.text){
      return this.list.filter(item=>JSON.stringify(item).includes(this.text))
      }
      else{
        return this.list
      }
    },
    renderList(){
      if(this.id){
      return this.list.find(item=>item.id===this.id)
      }else{
        return {id:'无',utime:'无',doctor:'无',results:'无',medicine:'无',}
      }
    }
  }
}
</script>

<style scoped>
  .my-table {
    border-collapse: collapse;
    width: 100%;
  }

  .my-table td, .my-table th {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: left;
  }

  .my-table th {
    background-color: #f2f2f2;
  }
</style>
